<!DOCTYPE html>
<html>
<head>
  <meta charset="utf-8">
  
  <title>mac mysql 安装配置以及忘记初始密码的解决方法 | Jin Tian</title>
  <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
  
  <meta name="keywords" content="web后台" />
  
  
  
  
  <meta name="description" content="本文详细说明了mac下mysql的安装和配置，现在mysql出于安全考虑安装时会默认初始化一个随机密码，如果忘记了需要重置也可以从本文找到答案。">
<meta name="keywords" content="web后台">
<meta property="og:type" content="article">
<meta property="og:title" content="Mac mysql 安装配置以及忘记初始密码的解决方法">
<meta property="og:url" content="http://yoursite.com/2016/11/30/Mac-mysql-安装配置以及忘记初始密码的解决方法/index.html">
<meta property="og:site_name" content="Jin Tian">
<meta property="og:description" content="本文详细说明了mac下mysql的安装和配置，现在mysql出于安全考虑安装时会默认初始化一个随机密码，如果忘记了需要重置也可以从本文找到答案。">
<meta property="og:locale" content="zh-CN">
<meta property="og:image" content="http://ofwzcunzi.bkt.clouddn.com/uyYThZ50NXdO0Hou.png">
<meta property="og:image" content="http://ofwzcunzi.bkt.clouddn.com/VYi0Ss4Rh7g3bhd4.png">
<meta property="og:image" content="http://ofwzcunzi.bkt.clouddn.com/e8O9q4MZzyPwhveL.png">
<meta property="og:updated_time" content="2017-01-12T11:47:11.000Z">
<meta name="twitter:card" content="summary">
<meta name="twitter:title" content="Mac mysql 安装配置以及忘记初始密码的解决方法">
<meta name="twitter:description" content="本文详细说明了mac下mysql的安装和配置，现在mysql出于安全考虑安装时会默认初始化一个随机密码，如果忘记了需要重置也可以从本文找到答案。">
<meta name="twitter:image" content="http://ofwzcunzi.bkt.clouddn.com/uyYThZ50NXdO0Hou.png">
  
    <link rel="alternate" href="/atom.xml" title="Jin Tian" type="application/atom+xml">
  

  

  <link rel="icon" href="/css/images/mylogo.jpg">
  <link rel="apple-touch-icon" href="/css/images/mylogo.jpg">
  
    <link href="//fonts.googleapis.com/css?family=Source+Code+Pro" rel="stylesheet" type="text/css">
  
  <link href="https://fonts.googleapis.com/css?family=Open+Sans|Montserrat:700" rel="stylesheet" type="text/css">
  <link href="https://fonts.googleapis.com/css?family=Roboto:400,300,300italic,400italic" rel="stylesheet" type="text/css">
  <link href="//cdn.bootcss.com/font-awesome/4.6.3/css/font-awesome.min.css" rel="stylesheet">
  <style type="text/css">
    @font-face{font-family:futura-pt;src:url(https://use.typekit.net/af/9749f0/00000000000000000001008f/27/l?subset_id=2&fvd=n5) format("woff2");font-weight:500;font-style:normal;}
    @font-face{font-family:futura-pt;src:url(https://use.typekit.net/af/90cf9f/000000000000000000010091/27/l?subset_id=2&fvd=n7) format("woff2");font-weight:500;font-style:normal;}
    @font-face{font-family:futura-pt;src:url(https://use.typekit.net/af/8a5494/000000000000000000013365/27/l?subset_id=2&fvd=n4) format("woff2");font-weight:lighter;font-style:normal;}
    @font-face{font-family:futura-pt;src:url(https://use.typekit.net/af/d337d8/000000000000000000010095/27/l?subset_id=2&fvd=i4) format("woff2");font-weight:400;font-style:italic;}</style>
  <link rel="stylesheet" href="/css/style.css">

  <script src="/js/jquery-3.1.1.min.js"></script>
  <script src="/js/bootstrap.js"></script>

  <!-- Bootstrap core CSS -->
  <link rel="stylesheet" href="/css/bootstrap.css" >

  
    <link rel="stylesheet" href="/css/dialog.css">
  

  

  
    <link rel="stylesheet" href="/css/header-post.css" >
  

  
  
  
    <link rel="stylesheet" href="/css/vdonate.css" >
  

</head>



  <body data-spy="scroll" data-target="#toc" data-offset="50">


  
  <div id="container">
    <div id="wrap">
      
        <header>

    <div id="allheader" class="navbar navbar-default navbar-static-top" role="navigation">
        <div class="navbar-inner">
          
          <div class="container"> 
            <button type="button" class="navbar-toggle" data-toggle="collapse" data-target=".navbar-collapse">
              <span class="sr-only">Toggle navigation</span>
              <span class="icon-bar"></span>
              <span class="icon-bar"></span>
              <span class="icon-bar"></span>
            </button>

            
              <a class="brand" style="
                 border-width: 0px;  margin-top: 0px;"  
                href="#" data-toggle="modal" data-target="#myModal" >
                  <img width="124px" height="124px" alt="Hike News" src="/css/images/mylogo.jpg">
              </a>
            
            
            <div class="navbar-collapse collapse">
              <ul class="hnav navbar-nav">
                
                  <li> <a class="main-nav-link" href="/">首页</a> </li>
                
                  <li> <a class="main-nav-link" href="/archives">归档</a> </li>
                
                  <li> <a class="main-nav-link" href="/categories">分类</a> </li>
                
                  <li> <a class="main-nav-link" href="/tags">标签</a> </li>
                
                  <li> <a class="main-nav-link" href="/about">关于</a> </li>
                
                  <li> <a class="main-nav-link" href="http://luoli-luoli.com/chat">chat</a> </li>
                
                  <li><div id="search-form-wrap">

    <form class="search-form">
        <input type="text" class="ins-search-input search-form-input" placeholder="" />
        <button type="submit" class="search-form-submit"></button>
    </form>
    <div class="ins-search">
    <div class="ins-search-mask"></div>
    <div class="ins-search-container">
        <div class="ins-input-wrapper">
            <input type="text" class="ins-search-input" placeholder="请输入关键词..." />
            <span class="ins-close ins-selectable"><i class="fa fa-times-circle"></i></span>
        </div>
        <div class="ins-section-wrapper">
            <div class="ins-section-container"></div>
        </div>
    </div>
</div>
<script>
(function (window) {
    var INSIGHT_CONFIG = {
        TRANSLATION: {
            POSTS: '文章',
            PAGES: '页面',
            CATEGORIES: '分类',
            TAGS: '标签',
            UNTITLED: '(无标题)',
        },
        ROOT_URL: '/',
        CONTENT_URL: '/content.json',
    };
    window.INSIGHT_CONFIG = INSIGHT_CONFIG;
})(window);
</script>
<script src="/js/insight.js"></script>

</div></li>
            </div>
          </div>
                
      </div>
    </div>

</header>



      
            
      <div id="content" class="outer">
        
          <section id="main" style="float:none;"><article id="post-Mac-mysql-安装配置以及忘记初始密码的解决方法" style="width: 75%; float:left;" class="article article-type-post" itemscope itemprop="blogPost" >
  <div id="articleInner" class="article-inner">
    
    
      <header class="article-header">
        
  
    <h1 class="thumb" class="article-title" itemprop="name">
      Mac mysql 安装配置以及忘记初始密码的解决方法
    </h1>
  

      </header>
    
    <div class="article-meta">
      
	<a href="/2016/11/30/Mac-mysql-安装配置以及忘记初始密码的解决方法/" class="article-date">
	  <time datetime="2016-11-30T04:04:10.000Z" itemprop="datePublished">2016-11-30</time>
	</a>

      
      
	<a class="article-views">
	<span id="busuanzi_container_page_pv">
		阅读量<span id="busuanzi_value_page_pv"></span>
	</span>
	</a>

    </div>
    <div class="article-entry" itemprop="articleBody">
      
        <p>本文详细说明了mac下mysql的安装和配置，现在mysql出于安全考虑安装时会默认初始化一个随机密码，如果忘记了需要重置也可以从本文找到答案。<br><a id="more"></a></p>
<blockquote>
<p>本文由作者金天原创，欢迎大家转载，不过请保留这段版权信息，多谢合作，如对本文有任何问题欢迎联系作者: jintianiloveu</p>
</blockquote>
<h3 id="Mac下mysql的安装（推荐使用dmg安装，避免不必要的麻烦）"><a href="#Mac下mysql的安装（推荐使用dmg安装，避免不必要的麻烦）" class="headerlink" title="Mac下mysql的安装（推荐使用dmg安装，避免不必要的麻烦）"></a>Mac下mysql的安装（推荐使用dmg安装，避免不必要的麻烦）</h3><p>Mac下安装mysql可以直接去官网下载dmg安装包，但是速度实在是慢，如果大家需要下载链接可以在下面回复邮箱我会第一时间打给大家，当然也可以加入我们的欧曼开发者联盟QQ群：373987228，中国新一代计算机高手聚集地。好了闲话不多说我们直接看看如何安装。</p>
<p>首先这个mysql如果用dmg安装的话就是傻瓜一键式安装了，非常方便简单快捷。dmg安装包默认安装sql在了/usr/local文件目录下：</p>
<p><img src="http://ofwzcunzi.bkt.clouddn.com/uyYThZ50NXdO0Hou.png" alt="PicName"></p>
<p>要重启或者停止mysql服务在设置中有：</p>
<p><img src="http://ofwzcunzi.bkt.clouddn.com/VYi0Ss4Rh7g3bhd4.png" alt="PicName"></p>
<p><img src="http://ofwzcunzi.bkt.clouddn.com/e8O9q4MZzyPwhveL.png" alt="PicName"></p>
<p>可以说非常的简单方便快捷。好了接下来我们该干啥？安装好了对不对？我们在终端输入mysql发现并没有什么卵用啊。同志们莫慌，这是因为，你还没有吧mysql加入环境变量，怎么加也很简单。</p>
<h3 id="Mysql加入环境变量"><a href="#Mysql加入环境变量" class="headerlink" title="Mysql加入环境变量"></a>Mysql加入环境变量</h3><p>我们在终端输入</p>
<figure class="highlight plain"><table><tr><td class="code"><pre><div class="line">vim ~/.bash_profile</div></pre></td></tr></table></figure>
<p>我们在里面增加一行：</p>
<figure class="highlight elixir"><table><tr><td class="code"><pre><div class="line">export PATH=<span class="variable">$PATH</span><span class="symbol">:/usr/local/mysql/bin</span></div></pre></td></tr></table></figure>
<p>这就把mysql添加进了环境变量，后面我们就可以用这个目录下的命令了。</p>
<h3 id="MySQL搭建好后忘记初始密码"><a href="#MySQL搭建好后忘记初始密码" class="headerlink" title="MySQL搭建好后忘记初始密码"></a>MySQL搭建好后忘记初始密码</h3><p>这是很多人遇到的问题，mysql安装时生成的密码又臭又长在终端输入根本看不到，很容易输错，而且很多时候我们搭建mysql的时候有妹子在旁边，跟妹子说话时就会忘记保存，这个时候我们就要重置密码了，在终端输入一下命令（先关闭mysql）：</p>
<figure class="highlight ada"><table><tr><td class="code"><pre><div class="line">sudo mysqld_safe <span class="comment">--skip-grant-tables</span></div></pre></td></tr></table></figure>
<p>这里mysqld_safe的意思是让mysql进入安全模式（麻瓜也知道），后面—skip-grant-tables意思是不需要密码进入，很容易理解吧，可以说这行命令很重要，大家在必要的时候可以记住，（如果你第二次忘记密码，那么你用这个命令的时候要关闭mysqld服务，怎么关闭Google一下）接着：</p>
<figure class="highlight ebnf"><table><tr><td class="code"><pre><div class="line"><span class="attribute">mysql -u root</span></div></pre></td></tr></table></figure>
<p>然后就用SQL语句修改密码：<br><strong>UPDATE 2017.1.12</strong></p>
<figure class="highlight sql"><table><tr><td class="code"><pre><div class="line"><span class="keyword">UPDATE</span> mysql.user <span class="keyword">SET</span> <span class="keyword">password</span>=<span class="keyword">PASSWORD</span>(<span class="string">'yourpassword'</span>) <span class="keyword">WHERE</span> <span class="keyword">User</span>=<span class="string">'root'</span>;</div></pre></td></tr></table></figure>
<p>这行命令应该改为：<br><figure class="highlight sql"><table><tr><td class="code"><pre><div class="line"><span class="keyword">UPDATE</span> mysql.user <span class="keyword">SET</span> authentication_string=<span class="keyword">PASSWORD</span>(<span class="string">'root'</span>) <span class="keyword">WHERE</span> <span class="keyword">user</span>=<span class="string">'root'</span>;</div></pre></td></tr></table></figure></p>
<p>因为在新版本mysql中默认保存用户名的密码表在数据库mysql下的user表中，密码保存字段变成了<strong>authentication_string</strong></p>
<p>OK!确定之后/c，然后exit退出，在开一个终端输入</p>
<figure class="highlight ebnf"><table><tr><td class="code"><pre><div class="line"><span class="attribute">mysql -u root -p</span></div></pre></td></tr></table></figure>
<p>接着输入新密码就可以进入mysql啦~</p>
<blockquote>
<p>感谢大家阅读我的博客，本文永久更新地址: jinfagang.coding.me</p>
<p>也欢迎大家积极留言，让我看到你的存在</p>
</blockquote>

      
    </div>
    <footer class="article-footer">
      
        <div id="donation_div"></div>

<script src="/js/vdonate.js"></script>
<script>
var a = new Donate({
  title: '骚年，加个好友打赏一下啊，现在连泡面都吃不起了啊', // 可选参数，打赏标题
  btnText: '打赏支持', // 可选参数，打赏按钮文字
  el: document.getElementById('donation_div'),
  wechatImage: 'https://i.loli.net/2017/09/27/59cb048ba6838.jpeg',
  alipayImage: 'https://i.loli.net/2017/09/27/59cb049cd0951.jpeg'
});
</script>
      
      
        
	<div id="comment">
		<!-- 来必力City版安装代码 -->
		<div id="lv-container" data-id="city" data-uid="MTAyMC8zMDA5MC82NjQ1">
		<script type="text/javascript">
		   (function(d, s) {
		       var j, e = d.getElementsByTagName(s)[0];

		       if (typeof LivereTower === 'function') { return; }

		       j = d.createElement(s);
		       j.src = 'https://cdn-city.livere.com/js/embed.dist.js';
		       j.async = true;

		       e.parentNode.insertBefore(j, e);
		   })(document, 'script');
		</script>
		<noscript>为正常使用来必力评论功能请激活JavaScript</noscript>
		</div>
		<!-- City版安装代码已完成 -->
	</div>



      
      
  <ul class="article-tag-list"><li class="article-tag-list-item"><a class="article-tag-list-link" href="/tags/web后台/">web后台</a></li></ul>

    </footer>
  </div>
  
    
<nav id="article-nav">
  
    <a href="/2016/12/01/Ubuntu16-04搭建Mxnet惊天地泣鬼神完整教程-深度学习起航/" id="article-nav-newer" class="article-nav-link-wrap">
      <strong class="article-nav-caption">上一篇</strong>
      <div class="article-nav-title">
        
          Ubuntu16.04搭建Mxnet惊天地泣鬼神完整教程,深度学习起航
        
      </div>
    </a>
  
  
    <a href="/2016/11/29/Hexo如何在两台电脑上更新博客/" id="article-nav-older" class="article-nav-link-wrap">
      <strong class="article-nav-caption">下一篇</strong>
      <div class="article-nav-title">Hexo如何在两台电脑上更新博客</div>
    </a>
  
</nav>

  
</article>

<!-- Table of Contents -->

  <aside id="toc-sidebar">
    <div id="toc" class="toc-article">
    <strong class="toc-title">文章目录</strong>
    
        <ol class="nav"><li class="nav-item nav-level-3"><a class="nav-link" href="#Mac下mysql的安装（推荐使用dmg安装，避免不必要的麻烦）"><span class="nav-number">1.</span> <span class="nav-text">Mac下mysql的安装（推荐使用dmg安装，避免不必要的麻烦）</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#Mysql加入环境变量"><span class="nav-number">2.</span> <span class="nav-text">Mysql加入环境变量</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#MySQL搭建好后忘记初始密码"><span class="nav-number">3.</span> <span class="nav-text">MySQL搭建好后忘记初始密码</span></a></li></ol>
    
    </div>
  </aside>
</section>
        
      </div>
      
      <footer id="footer">
  

  <div class="container">
      	<div class="row">
	      <p> Powered by <a href="http://www.luoli-luoli.com/" target="_blank">萝莉萝莉</a> and <a href="http://www.luoli-luoli.com/sia" target="_blank">Sia</a> </p>
	      <p id="copyRightEn">Copyright &copy; 2017 - 2018 Jin Tian All Rights Reserved.</p>
	      
	      
    		<p class="busuanzi_uv">
				访客数 : <span id="busuanzi_value_site_uv"></span> |  
				访问量 : <span id="busuanzi_value_site_pv"></span>
		    </p>
  		   
		</div>

		
  </div>
</footer>


<!-- min height -->

<script>
    var wrapdiv = document.getElementById("wrap");
    var contentdiv = document.getElementById("content");
    var allheader = document.getElementById("allheader");

    wrapdiv.style.minHeight = document.body.offsetHeight + "px";
    if (allheader != null) {
      contentdiv.style.minHeight = document.body.offsetHeight - allheader.offsetHeight - document.getElementById("footer").offsetHeight + "px";
    } else {
      contentdiv.style.minHeight = document.body.offsetHeight - document.getElementById("footer").offsetHeight + "px";
    }
</script>
    </div>
    <!-- <nav id="mobile-nav">
  
    <a href="/" class="mobile-nav-link">Home</a>
  
    <a href="/archives" class="mobile-nav-link">Archives</a>
  
    <a href="/categories" class="mobile-nav-link">Categories</a>
  
    <a href="/tags" class="mobile-nav-link">Tags</a>
  
    <a href="/about" class="mobile-nav-link">About</a>
  
    <a href="http://luoli-luoli.com/chat" class="mobile-nav-link">Chat</a>
  
</nav> -->
    

<!-- mathjax config similar to math.stackexchange -->

<script type="text/x-mathjax-config">
  MathJax.Hub.Config({
    tex2jax: {
      inlineMath: [ ['$','$'], ["\\(","\\)"] ],
      processEscapes: true
    }
  });
</script>

<script type="text/x-mathjax-config">
    MathJax.Hub.Config({
      tex2jax: {
        skipTags: ['script', 'noscript', 'style', 'textarea', 'pre', 'code']
      }
    });
</script>

<script type="text/x-mathjax-config">
    MathJax.Hub.Queue(function() {
        var all = MathJax.Hub.getAllJax(), i;
        for(i=0; i < all.length; i += 1) {
            all[i].SourceElement().parentNode.className += ' has-jax';
        }
    });
</script>

<script type="text/javascript" src="https://cdn.mathjax.org/mathjax/latest/MathJax.js?config=TeX-AMS-MML_HTMLorMML">
</script>


  <link rel="stylesheet" href="/fancybox/jquery.fancybox.css">
  <script src="/fancybox/jquery.fancybox.pack.js"></script>


<script src="/js/scripts.js"></script>




  <script src="/js/dialog.js"></script>








	<div style="display: none;">
    <script src="https://s95.cnzz.com/z_stat.php?id=1260716016&web_id=1260716016" language="JavaScript"></script>
  </div>



	<script async src="//dn-lbstatics.qbox.me/busuanzi/2.3/busuanzi.pure.mini.js">
	</script>






  </div>

  <div class="modal fade" id="myModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true" style="display: none;">
  <div class="modal-dialog">
    <div class="modal-content">
      <div class="modal-header">
        <h2 class="modal-title" id="myModalLabel">设置</h2>
      </div>
      <hr style="margin-top:0px; margin-bottom:0px; width:80%; border-top: 3px solid #000;">
      <hr style="margin-top:2px; margin-bottom:0px; width:80%; border-top: 1px solid #000;">


      <div class="modal-body">
          <div style="margin:6px;">
            <a data-toggle="collapse" data-parent="#accordion" href="#collapseOne" onclick="javascript:setFontSize();" aria-expanded="true" aria-controls="collapseOne">
              正文字号大小
            </a>
          </div>
          <div id="collapseOne" class="panel-collapse collapse" role="tabpanel" aria-labelledby="headingOne">
          <div class="panel-body">
            您已调整页面字体大小
          </div>
        </div>
      


          <div style="margin:6px;">
            <a data-toggle="collapse" data-parent="#accordion" href="#collapseTwo" onclick="javascript:setBackground();" aria-expanded="true" aria-controls="collapseTwo">
              夜间护眼模式
            </a>
        </div>
          <div id="collapseTwo" class="panel-collapse collapse" role="tabpanel" aria-labelledby="headingTwo">
          <div class="panel-body">
            夜间模式已经开启，再次单击按钮即可关闭 
          </div>
        </div>

        <div>
            <a data-toggle="collapse" data-parent="#accordion" href="#collapseThree" aria-expanded="true" aria-controls="collapseThree">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;关 于&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</a>
        </div>
         <div id="collapseThree" class="panel-collapse collapse" role="tabpanel" aria-labelledby="headingThree">
          <div class="panel-body">
            Jin Tian
          </div>
          <div class="panel-body">
            Copyright © 2018 Jintian All Rights Reserved.
          </div>
        </div>
      </div>


      <hr style="margin-top:0px; margin-bottom:0px; width:80%; border-top: 1px solid #000;">
      <hr style="margin-top:2px; margin-bottom:0px; width:80%; border-top: 3px solid #000;">
      <div class="modal-footer">
        <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">×</span></button>
      </div>
    </div>
  </div>
</div>
  
  <a id="rocket" href="#top" class=""></a>
  <script type="text/javascript" src="/js/totop.js?v=1.0.0" async=""></script>
  
    <a id="menu-switch"><i class="fa fa-bars fa-lg"></i></a>
  
</body>
</html>